package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import form.ConnectionDB;

public class RoleDAO {

	public static boolean addRole(String ten,String ho,String email,String matkhau,int gioitinh,Date sinhnhat,int capdo) throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "insert into users(ten,ho,email,matkhau,gioitinh,sinhnhat,capdo) values (?,?,?,?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, ten);
		ps.setString(2, ho);
		ps.setString(3, email);
		ps.setString(4, matkhau);
		ps.setInt(5, gioitinh);
		ps.setDate(6, sinhnhat);
		ps.setInt(7, capdo);
	
		int change = 0;
		change = ps.executeUpdate();
		connection.close();
		if(change >0){
			return true;
		}else{
			return false;
		}
	}
	
	public static int getRole(String email) throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		String sql = "select users.capdo from users where email=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, email);
		int sold =0;
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			sold = rs.getInt("capdo");
		}
		connection.close();
		return sold;
	}
	
	public static ArrayList<String> getEmail(int role1,int role2) throws ClassNotFoundException, SQLException {
		Connection connection = ConnectionDB.getConnection();
		ArrayList<String> result = new ArrayList<>();
		String sql = "select email from users where(capdo=? or capdo=?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setInt(1, role1);
		ps.setInt(2, role2);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			String email = rs.getString("email");
			result.add(email);
			
		}
		connection.close();
		return result;
		
	}
}
